/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HedgehogPanel.java
 *
 * Created on 7-okt-2009, 20:20:49
 */

package gui;

import flowvis.ColorMap;
import flowvis.ForceRenderer;
import flowvis.HedgehogRenderer;
import flowvis.VelocityRenderer;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 *
 * @author michel
 */
public class HedgehogRendererPanel extends javax.swing.JPanel {

    HedgehogRenderer renderer;
    // Color legend: starting point, width, height and space between values.
    private static int xi = 20, yi=210, w = 30, h=256, dt=20;
    private static int l, dy, dt1, dt2;
    private static Font font = new Font("Serif", Font.PLAIN, 12);
    private static float values[];

    /** Creates new form HedgehogPanel */
    public HedgehogRendererPanel(HedgehogRenderer renderer) {
        initComponents();
        this.renderer = renderer;
        scalingTextfield.setText("" + HedgehogRenderer.getScale());
        colorComboBox.setSelectedIndex(HedgehogRenderer.getColor());
    }

    /**
     * @param nv , number of values.
     */
    private static void paintValues( int nv, Graphics2D g2d ){
        values = new float[nv];
        int i;
        if( !HedgehogRenderer.getVectorField() ){
            values[0] = (float)VelocityRenderer.MAX_USER_VELOCITY;
            values[nv-1]= (float)VelocityRenderer.MIN_USER_VELOCITY;
            for( i=1;i<nv-1;i++ )
                values[i] = values[i-1]-(float)((VelocityRenderer.MAX_USER_VELOCITY-VelocityRenderer.MIN_USER_VELOCITY)/(float)(nv-1));
        } else {
            values[0] = (float)ForceRenderer.MAX_USER_FORCE;
            values[nv-1]= (float)ForceRenderer.MIN_USER_FORCE;
            for( i=1;i<nv-1;i++ )
                values[i] = values[i-1]-(float)((ForceRenderer.MAX_USER_FORCE-ForceRenderer.MIN_USER_FORCE)/(float)(nv-1));
        }
        // Paint
        dt1=yi+10+l*dy;
        g2d.setFont(font);
        g2d.drawString(String.valueOf(values[nv-1]), xi+w+2, dt1);
        for( i=l-1;i>=0;i-- ){
            dt2=yi+10+i*dy;
            if( dt1-dt2>dt ){
                g2d.drawString(String.valueOf(values[i]), xi+w+2, dt2);
                dt1=dt2;
            }
        }
    }

    /**
     * Paint the color legend
     * @param g2d , standard Java2D drawing object.
     * @param colorMap to use.
     */
    private static void paintColorLegend( Graphics2D g2d, int colorMap ){
        // color and value to create the l colors.
        float rgb[] = new float[3], v;
        // Obtain the height of the color box
        dy = (int)Math.ceil(h/(l+1));
        // Obtain the values and paint some of them
        paintValues( (int)Math.ceil(256/dy), g2d );
        // Paint colors
        for( int i=l;i>=0;i-- ){
            // From v, obtain the rgb color
            v=1.0f-((float)i/((float)(l)));
            switch( colorMap ){
                case 0:
                    ColorMap.white(v, rgb, false, false );
                    break;
                case 1:
                    ColorMap.blackwhite(v, rgb, false, false );
                    break;
                case 2:
                    ColorMap.rainbow(v, rgb, false, false );
                    break;
                case 3:
                    ColorMap.blackbody(v, rgb, false, false );
                    break;
                case 4:
                    ColorMap.temperature(v, rgb, false, false );
                    break;
                case 5:
                    ColorMap.green2red(v, rgb, false, false );
                    break;
            }
            g2d.setPaint( new Color(rgb[0],rgb[1],rgb[2]) );
            g2d.fillRect(xi, yi-1+i*dy, w, dy);
        }
    }

    /**
     * Painting of color legend.
     */
    @Override public void paintComponent( Graphics g ){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        l = 255;
        switch( HedgehogRenderer.getColor() ){
            case 0:
                paintColorLegend(g2d, 0);
                break;
            case 1:
                paintColorLegend(g2d, 1);
                break;
            case 2:
                paintColorLegend(g2d, 2);
                break;
            case 3:
                paintColorLegend(g2d, 3);
                break;
            case 4:
                paintColorLegend(g2d, 4);
                break;
            case 5:
                paintColorLegend(g2d, 5);
                break;
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        scalingTextfield = new javax.swing.JTextField();
        colorComboBox = new javax.swing.JComboBox();
        colorMappingLabel = new javax.swing.JLabel();
        NumberOfXSlider = new javax.swing.JSlider();
        NbXTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        nbOfYTextField = new javax.swing.JTextField();
        NumberOfYSlider = new javax.swing.JSlider();

        setPreferredSize(new java.awt.Dimension(473, 285));

        jLabel1.setText("Vector scaling:");

        scalingTextfield.setText(String.valueOf( HedgehogRenderer.getScale() ));
        scalingTextfield.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scalingTextfieldActionPerformed(evt);
            }
        });

        colorComboBox.setMaximumRowCount(5);
        colorComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "White", "Grey scale", "Rainbow", "Black body", "Temperature", "Green to Red" }));
        colorComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colorComboBoxActionPerformed(evt);
            }
        });

        colorMappingLabel.setText("Color mapping:");

        NumberOfXSlider.setMaximum(64);
        NumberOfXSlider.setSnapToTicks(true);
        NumberOfXSlider.setToolTipText("Drag to change the number of X");
        NumberOfXSlider.setValue(64);
        NumberOfXSlider.setName("numberOfX_slide"); // NOI18N
        NumberOfXSlider.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                NumberOfXSliderMouseDragged(evt);
            }
        });

        NbXTextField.setEditable(false);
        NbXTextField.setText("64");

        jLabel3.setText("Number of X:");

        jLabel4.setText("Number of Y:");

        nbOfYTextField.setEditable(false);
        nbOfYTextField.setText("64");
        nbOfYTextField.setToolTipText("");

        NumberOfYSlider.setMaximum(64);
        NumberOfYSlider.setToolTipText("Drag to change the number of Y");
        NumberOfYSlider.setValue(64);
        NumberOfYSlider.setName("numberOfYSlider"); // NOI18N
        NumberOfYSlider.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                NumberOfYSliderMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(scalingTextfield, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(NumberOfXSlider, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
                                        .addComponent(NbXTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                                        .addComponent(nbOfYTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                                        .addComponent(NumberOfYSlider, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(colorMappingLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                                .addComponent(colorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(188, 188, 188))
                    .addComponent(jLabel4))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(colorMappingLabel)
                    .addComponent(colorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(scalingTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(NbXTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(NumberOfXSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(nbOfYTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(NumberOfYSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(113, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void scalingTextfieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scalingTextfieldActionPerformed
        renderer.setScale(Float.parseFloat(scalingTextfield.getText()));
    }//GEN-LAST:event_scalingTextfieldActionPerformed

private void colorComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colorComboBoxActionPerformed
        HedgehogRenderer.setColor( colorComboBox.getSelectedIndex() );
        this.updateUI();
}//GEN-LAST:event_colorComboBoxActionPerformed

private void NumberOfXSliderMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_NumberOfXSliderMouseDragged
    int nbX = NumberOfXSlider.getValue();
    HedgehogRenderer.setNbX(nbX);
    NbXTextField.setText(String.valueOf(nbX));
}//GEN-LAST:event_NumberOfXSliderMouseDragged

private void NumberOfYSliderMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_NumberOfYSliderMouseDragged
    int nbY = NumberOfYSlider.getValue();
    HedgehogRenderer.setNbY(nbY);
    nbOfYTextField.setText(String.valueOf(nbY));
}//GEN-LAST:event_NumberOfYSliderMouseDragged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField NbXTextField;
    private javax.swing.JSlider NumberOfXSlider;
    private javax.swing.JSlider NumberOfYSlider;
    private javax.swing.JComboBox colorComboBox;
    private javax.swing.JLabel colorMappingLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField nbOfYTextField;
    private javax.swing.JTextField scalingTextfield;
    // End of variables declaration//GEN-END:variables

}
